package org.spitter.core.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.spitter.core.dao.SpittleDao;
import org.spitter.core.model.Spitter;
import org.spitter.core.model.Spittle;
import org.springframework.stereotype.Repository;

@Repository
public class SpittleDaoImpl extends EntityJpaDaoImpl<Spittle> implements
		SpittleDao {

	public SpittleDaoImpl() {
		setClazz(Spittle.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Spittle> getRecentSpittles(int no) {
		Query q = entityManager.createQuery("FROM "
				+ clazz.getName() + " ORDER BY whenCol DESC LIMIT 0, " + no );
		List<Spittle> list = new ArrayList<Spittle>();
		try {
			list = q.getResultList();
		} catch (Exception e) {
		}
		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Spittle> findBySpitter(Spitter spitter) {
		Query q = entityManager.createQuery("FROM " + clazz.getName()
				+ " WHERE spitter_id=" +spitter.getId()+" ORDER BY whenCol DESC");
		List<Spittle> list = new ArrayList<Spittle>();
		try {
			list = q.getResultList();
		} catch (Exception e) {
		}
		return list;
	}
}
